 |
|
 |
Subject: Create image attachment from base64 encoded text |
 |
 |
 |
Product Area: Domino Server |
 |
Technical Area: Application Development |
 |
Platform: Not Applicable |
 |
Release: 8.5 |
 |
Reproducible: Always |
 |
 |
 |
 |
Hello,
I have a requirement to translate some base64 encoded text back into an image on the Domino server - currently trying with lotusscript in a webquerysave agent. I have pasted the base64 encoded text into an online utility that converts to an image and can successfully see the image, so looks like the text is ok. However when I try using the lotusscript webquery save code (see below) it does create the image as an attachment, but I cannot view it - I get an error saying "Can't read file header. Unknown file format, empty/damaged file or file not found.".
The actual problem I'm trying to solve is that I have a form that will be used on the iPad to upload photos but I am using javascript on the form to compress the image so that the post does not have to post really large images, with response times of over a minute. Currently the javascript code reduces the image and provides the base64 encoded text for it, so I am posting that, and was hoping the web query save agent could translate it back to an image and attach to the form.
I've done some searching and seems that this is achievable, however it is not working for me. I have literally copied this code from someone else's code where it supposedly worked. I found slightly different varieties of code and have tried quite a few and I keep getting the same error when I try to open the attachment.
If anyone has any ideas on what I am doing wrong, I would much appreciate the feedback!
Sub Initialize
On Error Goto errHandler
Dim session As New NotesSession
Dim db As NotesDatabase
Dim doc As NotesDocument
Dim stream As NotesStream
Dim rtItem As NotesRichTextItem
Dim header As NotesMIMEHeader
Dim parent As NotesMimeEntity
Dim child As NotesMimeEntity
Dim filename As String
Dim strURL As String
Dim strReturnPage As String
Set doc = session.DocumentContext
Set db = session.CurrentDatabase
session.ConvertMime=False
filename = "image.png"
Set stream = session.CreateStream
'get a handle on the picture data in the rich text item
Set rtItem = doc.GetFirstItem("data")
'write the text data into the stream
Call stream.WriteText(rtItem.GetUnformattedText)
'create the new mime entity in the document
Set parent = doc.GetMIMEEntity("image")
If parent Is Nothing Then
Set parent = doc.CreateMIMEEntity("image")
End If
'create the new child
Set child = parent.CreateChildEntity()
'set the header information so we can name the file "image.png"
Set header = child.CreateHeader("Content-Disposition")
Call header.SetHeaderVal({attachment; filename="} & fileName & {"})
'calling SetContentFromText will attach the file into the document.
Call child.SetContentFromText(stream, "image/png", ENC_BASE64)
Call child.DecodeContent()
'save the changes.
Call doc.Save(False,False)
stream.Close
'// User navigation
strReturnPage = Strright( doc.HTTP_Cookie(0), "view=" )
If Instr(strReturnPage, ";") <> 0 Then
strReturnPage = Strleft( strReturnPage, ";")
End If
Print "[/" & db.FilePath & "/" + strReturnPage + "]"
Exit Sub
errHandler:
'// the "~" character is converted into a <br /> (i.e a new line) on the resultant Error Page
Dim strErrorMsg As String
strErrorMsg = "Agent: " & session.CurrentAgent.Name &_
"~Error Number: " & Cstr(Err) &_
"~Message: " & Error$ &_
"~Line: " & Cstr(Erl) & " in Sub " & Lsi_info(2) & "~]"
'write to agent error log
Call WriteLog( Lsi_info(2) )
Print "[/" & session.CurrentDatabase.FilePath & "/$$ReturnGeneralError?Open&ErrMsg=" & strErrorMsg
Exit Sub
End Sub
 
Feedback number WEBBADY2XN created by ~Zach Optoolitettu on 09/20/2016

Status: Open
Comments:

|
|  |
|